<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="120px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="12" class="mb20">
					<el-form-item label="项目名称" prop="projectId">
						<el-select v-model="form.projectId" placeholder="请选择项目名称" @change="handleProjectChange">
							<el-option :key="index" :label="item.name" :value="item.id" v-for="(item, index) in projectData" />
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="年月" prop="year">
						<el-date-picker
							type="month"
							placeholder="请选择年月"
							v-model="form.yearMonth"
							value-format="YYYY-MM"
							@change="monthChange()"
						></el-date-picker>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="费用类型" prop="costTypeId">
						<el-select v-model="form.costTypeId" placeholder="请选择费用类型">
							<el-option :key="index" :label="item.name" :value="item.id" v-for="(item, index) in filteredCostTypeData" />
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="明细" prop="detail">
						<el-input v-model="form.detail" placeholder="请输入明细" />
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="金额" prop="amount">
						<el-input v-model="form.amount" type="number" placeholder="请输入金额">
							<template v-slot:append>元</template>
						</el-input>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="类型" prop="type">
						<el-select v-model="form.type" placeholder="请选择类型">
							<el-option :key="index" :label="item.label" :value="item.value" v-for="(item, index) in internal_payment_type" />
						</el-select>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="是否成本外" prop="costFlag">
						<el-radio-group v-model="form.costFlag">
							<el-radio :key="index" :label="item.value" border v-for="(item, index) in yes_no_type">{{ item.label }}</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>

				<el-col :span="12" class="mb20">
					<el-form-item label="供应商" prop="supplierName">
						<el-input v-model="form.supplierName" placeholder="请输入供应商" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="IntProjectCostActualDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj } from '/@/api/internal/cost/intProjectCostActual';
import { projectList, costTypeList } from '/@/api/internal/cost/intProject';
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);

const projectData = ref<any[]>([]);
const costTypeData = ref<any[]>([]);
// 定义字典
const { yes_no_type, internal_expense_category, internal_payment_type } = useDict(
	'yes_no_type',
	'internal_expense_category',
	'internal_payment_type'
);

// 提交表单数据
const form = reactive({
	id: '',
	projectId: '',
	yearMonth: '',
	year: '',
	month: '',
	costTypeId: '',
	detail: '',
	amount: '',
	type: '',
	costFlag: '0',
	supplierName: '',
});

// 定义校验规则
const dataRules = ref({
	projectId: [{ required: true, message: '项目id不能为空', trigger: 'blur' }],
	year: [{ required: true, message: '年度不能为空', trigger: 'blur' }],
	costTypeId: [{ required: true, message: '成本项id不能为空', trigger: 'blur' }],
	detail: [{ required: true, message: '明细不能为空', trigger: 'blur' }],
	amount: [
		{ required: true, message: '金额不能为空', trigger: 'blur' },
		{ validator: rule.number, trigger: 'blur' },
	],
	type: [{ required: true, message: '类型不能为空', trigger: 'blur' }],
	costFlag: [{ required: true, message: '是否成本外不能为空', trigger: 'blur' }],
	supplierName: [{ required: true, message: '供应商不能为空', trigger: 'blur' }],
});

const filteredCostTypeData = computed(() => {
	return costTypeData.value.filter((item) => item.selected == 1);
});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.id = '';

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取intProjectCostActual信息
	if (id) {
		form.id = id;
		getintProjectCostActualData(id);
	}

	getProjectData();
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getintProjectCostActualData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			Object.assign(form, res.data);
			getCostTypeList();
		})
		.finally(() => {
			loading.value = false;
		});
};

// 获取项目列表数据
const getProjectData = () => {
	projectList().then((res) => {
		projectData.value = res.data;
	});
};

// 月度选择
const monthChange = () => {
	const time = new Date(form.yearMonth);
	form.year = String(time.getFullYear());
	form.month = String(time.getMonth() + 1);
};

// 所属项目联动
const handleProjectChange = async () => {
	costTypeData.value = [];
	form.costTypeId = '';
	getCostTypeList();
};

const getCostTypeList = () => {
	costTypeList({ id: form.projectId }).then((res) => {
		costTypeData.value = res.data;
	});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
